﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Novel.Sys.Model;
using AdinZ;

namespace Novel.Utils
{
    public class CookieUtil
    {
        /// <summary>
        /// 保存cookie
        /// </summary>
        /// <param name="user"></param>
        /// <param name="CookieTime"></param>
        public static void SaveCookie(novel_user user,double CookieTime)
        {
            if (user.Gid == 10)
            {
                AdinZ.Common.Cookie.SaveCookie(AdinZ.Common.Constant.Cookies.admin_gid, user.Gid.ToString(), CookieTime);
                AdinZ.Common.Cookie.SaveCookie(AdinZ.Common.Constant.Cookies.admin_id, user.ID.ToString(), CookieTime);
                AdinZ.Common.Cookie.SaveCookie(AdinZ.Common.Constant.Cookies.admin_name, user.Name.ToString(), CookieTime);
                AdinZ.Common.Cookie.SaveCookie(AdinZ.Common.Constant.Cookies.admin_nick, user.Nick.ToString(), CookieTime);
                AdinZ.Common.Cookie.SaveCookie(AdinZ.Common.Constant.Cookies.admin_passwd, user.Passwd.ToString(), CookieTime);
            }
            else
            {
                AdinZ.Common.Cookie.SaveCookie(AdinZ.Common.Constant.Cookies.user_gid, user.Gid.ToString(), CookieTime);
                AdinZ.Common.Cookie.SaveCookie(AdinZ.Common.Constant.Cookies.user_id, user.ID.ToString(), CookieTime);
                AdinZ.Common.Cookie.SaveCookie(AdinZ.Common.Constant.Cookies.user_name, user.Name.ToString(), CookieTime);
                AdinZ.Common.Cookie.SaveCookie(AdinZ.Common.Constant.Cookies.user_nick, user.Nick.ToString(), CookieTime);
                AdinZ.Common.Cookie.SaveCookie(AdinZ.Common.Constant.Cookies.user_passwd, user.Passwd.ToString(), CookieTime);
            }
            
        }

        /// <summary>
        /// 删除cookie
        /// </summary>
        /// <param name="gid"></param>
        public static void DelCookie(int gid)
        {
            if (gid == 10)
            {
                AdinZ.Common.Cookie.ClearCookie(AdinZ.Common.Constant.Cookies.admin_gid);
                AdinZ.Common.Cookie.ClearCookie(AdinZ.Common.Constant.Cookies.admin_id);
                AdinZ.Common.Cookie.ClearCookie(AdinZ.Common.Constant.Cookies.admin_name);
                AdinZ.Common.Cookie.ClearCookie(AdinZ.Common.Constant.Cookies.admin_nick);
                AdinZ.Common.Cookie.ClearCookie(AdinZ.Common.Constant.Cookies.admin_passwd);
            }
            else
            {
                AdinZ.Common.Cookie.ClearCookie(AdinZ.Common.Constant.Cookies.user_gid);
                AdinZ.Common.Cookie.ClearCookie(AdinZ.Common.Constant.Cookies.user_id);
                AdinZ.Common.Cookie.ClearCookie(AdinZ.Common.Constant.Cookies.user_name);
                AdinZ.Common.Cookie.ClearCookie(AdinZ.Common.Constant.Cookies.user_nick);
                AdinZ.Common.Cookie.ClearCookie(AdinZ.Common.Constant.Cookies.user_passwd);
            }
        }
        #region 获取管理员cookie
        public static int GetAdminGid()
        {
            try
            {
                return int.Parse(AdinZ.Common.Cookie.GetCookie(AdinZ.Common.Constant.Cookies.admin_gid));
            }
            catch { return 0; }
        }
        public static int GetAdminId()
        {
            try
            {
                return int.Parse(AdinZ.Common.Cookie.GetCookie(AdinZ.Common.Constant.Cookies.admin_id));
            }
            catch { return 0; }
        }
        public static string GetAdminName()
        {
            return AdinZ.Common.Cookie.GetCookie(AdinZ.Common.Constant.Cookies.admin_name);
        }
        public static string GetAdminPasswd()
        {
            return AdinZ.Common.Cookie.GetCookie(AdinZ.Common.Constant.Cookies.admin_passwd);
        }
        public static string GetAdminNick()
        {
            return AdinZ.Common.Cookie.GetCookie(AdinZ.Common.Constant.Cookies.admin_nick);
        }
        #endregion

        #region 获取普通会员cookie
        public static int GetUserGid()
        {
            try
            {
                return int.Parse(AdinZ.Common.Cookie.GetCookie(AdinZ.Common.Constant.Cookies.user_gid));
            }
            catch { return 0; }
        }
        public static int GetUserId()
        {
            try
            {
                return int.Parse(AdinZ.Common.Cookie.GetCookie(AdinZ.Common.Constant.Cookies.user_id));
            }
            catch { return 0; }
        }
        public static string GetUserName()
        {
            return AdinZ.Common.Cookie.GetCookie(AdinZ.Common.Constant.Cookies.user_name);
        }
        public static string GetUserPasswd()
        {
            return AdinZ.Common.Cookie.GetCookie(AdinZ.Common.Constant.Cookies.user_passwd);
        }
        public static string GetUserNick()
        {
            return AdinZ.Common.Cookie.GetCookie(AdinZ.Common.Constant.Cookies.user_nick);
        }

        #endregion


    }
}
